package com.biz.primus.ms.sms.service.checker;

import com.biz.primus.model.misc.enums.ApiExceptionType;
import com.biz.primus.model.misc.exception.ApiException;
import com.biz.primus.model.misc.vo.ChannelBasedAuth;
import com.biz.primus.model.misc.vo.ShortMessageVo;
import com.biz.primus.ms.sms.event.ApiExceptionEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

/**
 * @author yanweijin
 * @date 2017/7/7
 */
@Component
public class ExpiredRequestChecker extends SendCheckerAdapter{


	@Autowired
	private ApplicationEventPublisher publisher;

	@SuppressWarnings("deprecation")
	@Override
	public void check(ChannelBasedAuth cba) throws ApiException {
		if(cba instanceof ShortMessageVo){
			ShortMessageVo req = (ShortMessageVo) cba;
			if(req.getExpireAt()!=null && System.currentTimeMillis() >= req.getExpireAt().getTime()){
				String extraMessage = "请求已过期,传入的请求过期时间为:" + req.getExpireAt().toLocaleString();
				publisher.publishEvent(new ApiExceptionEvent(this,req, ApiExceptionType.REQUEST_EXPIRED,extraMessage));
				throw new ApiException(extraMessage,ApiExceptionType.REQUEST_EXPIRED);
			}
		}
	}
}
